package com.example.demo.services;

import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.models.ActivityUser;
import com.example.demo.models.user.User;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.contract.eloquent.RecordList;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lucifer
 * @since 2024-02-01-11:42
 */

public class ActivityUserService {

    private static final ActivityUser.Model activityUserModel;

    private static final User.Model userModel;

    static {
        activityUserModel = SpringUtil.getBean(ActivityUser.Model.class);
        userModel = SpringUtil.getBean(User.Model.class);
    }

    public static void exportExcel(String fileName, Record<ActivityUser, Long> activityUser, HttpServletResponse response) {
        RecordList<ActivityUser, Long> record = activityUserModel.newQuery().
                where("activity_id", activityUser.getEntity().getActivityId()).select("user_id").get();
        List<String> userIdList = new ArrayList<>();
        for (Record<ActivityUser, Long> activityUserLongRecord : record) {
            userIdList.add(activityUserLongRecord.getEntity().getUserId());
        }
        //改为人为构造
        RecordList<User, Long> list = userModel.newQuery().whereIn("id",userIdList).select("name", "number").get();
        List<Map<String, Object>> userList = new ArrayList<>();
        list.forEach(userLongRecord -> {
            Map<String, Object> map = new LinkedHashMap<>();
            User user = userLongRecord.getEntity();
            map.put("number", user.getNumber());
            map.put("name", user.getName());
            userList.add(map);
        });
//        List<Map<String, Object>> userList = userModel.newQuery().select("name", "number").
//                queryList("id", userIdList).
//                stream().
//                map(user -> {
//                    Map<String, Object> map = new LinkedHashMap<>();
//                    map.put("name", user.getEntity().getName());
//                    map.put("number", user.getEntity().getNumber());
//                    return map;
//                }).collect(Collectors.toList());

        // 准备将数据集合封装成Excel对象
        ExcelWriter writer = ExcelUtil.getWriter(true);

        // 通过工具类创建writer并且进行别名
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("number", "学号");

        // 准备将对象写入 List
        writer.write(userList, true);

        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            String resultFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
            response.setHeader("Content-disposition", "attachment;filename=" + resultFileName);
            // 获取输出流
            OutputStream output = response.getOutputStream();
            writer.flush(output, true);
            writer.close();
            IoUtil.close(output);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
